package com.practicesuite.controllayer;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JPasswordField;

import com.practicesuite.systemlayer.Admin;
import com.practicesuite.systemlayer.Doctor;
import com.practicesuite.systemlayer.DoctorOrder;
import com.practicesuite.systemlayer.Nurse;
import com.practicesuite.systemlayer.Patient;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.TreatmentRecord;
import com.practicesuite.systemlayer.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.awt.FlowLayout;

/**
 * Initial panel with options for logging in or signing up.
 * 
 * @author James
 *
 */
public class LoginPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTextField textFieldUsername;
	private JPasswordField passwordFieldPassword;
	public MainWindow window;
	
	// temporary patient object stuff
	public User user;
	public Patient patient;
	public Nurse nurse;
	public Doctor doctor;
	public Admin admin;
	public TreatmentRecord treatmentRecord1;
    public TreatmentRecord treatmentRecord2;
    public TreatmentRecord treatmentRecord3;
    public TreatmentRecord treatmentRecord4;
	public List<TreatmentRecord> medicalHistory = new ArrayList<TreatmentRecord>();
	public Date dateTime1;
    public Date dateTime2;
    public Date dateTime3;
    public Date dateTime4;
    public DoctorOrder docOrder1;
    public DoctorOrder docOrder2;
    public DoctorOrder docOrder3;
    public DoctorOrder docOrder4;
	
	private int tries = 0;

	/**
	 * Create the panel.
	 */
	public LoginPanel(MainWindow container) {
		this.window = container;
		setLayout(new GridLayout(0, 1));
        setPreferredSize(new Dimension(500, 400));
		
		JLabel labelHeader = new JLabel("Login");
		labelHeader.setFont(new Font("Tahoma", Font.PLAIN, 24));
		labelHeader.setHorizontalAlignment(SwingConstants.CENTER);
		add(labelHeader);
		
		JLabel labelNameEntry = new JLabel("Please enter username and password:");
		labelNameEntry.setFont(new Font("Tahoma", Font.PLAIN, 16));
		labelNameEntry.setVerticalAlignment(SwingConstants.BOTTOM);
		add(labelNameEntry);
		labelNameEntry.setHorizontalAlignment(SwingConstants.CENTER);
		
		JPanel panel_3 = new JPanel();
		add(panel_3);
		panel_3.setLayout(new GridLayout(0, 1, 0, 0));
		
		JPanel panel_4 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_4.getLayout();
		flowLayout.setVgap(1);
		panel_3.add(panel_4);
		
		JLabel labelUsername = new JLabel("Username: ");
		panel_4.add(labelUsername);
		labelUsername.setFont(new Font("Tahoma", Font.PLAIN, 12));
		labelUsername.setHorizontalAlignment(SwingConstants.RIGHT);
		
		textFieldUsername = new JTextField();
		panel_4.add(textFieldUsername);
		textFieldUsername.setFont(new Font("Tahoma", Font.PLAIN, 12));
		textFieldUsername.setColumns(15);
		
		JPanel panel_5 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_5.getLayout();
		flowLayout_1.setVgap(1);
		panel_3.add(panel_5);
		
		JLabel labelPassword = new JLabel("Password: ");
		panel_5.add(labelPassword);
		labelPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
		labelPassword.setHorizontalAlignment(SwingConstants.RIGHT);
		
		passwordFieldPassword = new JPasswordField();
		panel_5.add(passwordFieldPassword);
		passwordFieldPassword.setColumns(15);
		passwordFieldPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		JPanel panel_1 = new JPanel();
		add(panel_1);
		
		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String name = textFieldUsername.getText();
				char[] pass = passwordFieldPassword.getPassword();
				tries += 1;
				try {
    				SystemDriver.getInstance();
    				int eUID = SystemDriver.requestLogin(name, pass, tries);
    				if (eUID == 0) {
                        tries = 0;
                        admin = SystemDriver.getAdmin(name, pass);
                        window.gotoPanel("adminHome", admin);
                    }
                    else if (eUID == 1) {
                        tries = 0;
                        doctor = SystemDriver.getDoctor(name, pass);
                        window.gotoPanel("doctorHome", doctor);
                    }
                    else if (eUID == 2) {
                        tries = 0;
                        nurse = SystemDriver.getNurse(name, pass);
                        window.gotoPanel("nurseHome", nurse);
                    }
    				else if (eUID == 3) {
				        tries = 0;
                        patient = SystemDriver.getPatient(name);
                        medicalHistory = SystemDriver.getTreatmentRecordList(patient);
				        patient.setMedicalHistory(medicalHistory);
				        window.gotoPanel("patientHome", patient);
    				}
				} catch (Exception e) {
				    JOptionPane.showMessageDialog(null, e.getMessage());
				    e.printStackTrace();
				    passwordFieldPassword.setText("");
				}
			}
		});
		btnLogin.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_1.add(btnLogin);
		
		JLabel lblNewPatientRegister = new JLabel("New patient? Register here:");
		lblNewPatientRegister.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblNewPatientRegister.setVerticalAlignment(SwingConstants.BOTTOM);
		lblNewPatientRegister.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblNewPatientRegister);
		
		JPanel panel_2 = new JPanel();
		add(panel_2);
		
		JButton btnSignUp = new JButton("Sign Up");
		btnSignUp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			    window.gotoPanel("registration");
			}
		});
		btnSignUp.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel_2.add(btnSignUp);
		
		JPanel panel_6 = new JPanel();
		add(panel_6);
		
		JPanel panel = new JPanel();
		add(panel);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
	}
}
